package com.mec.web.y2012.coreservlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.NumberFormat;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

import com.mec.beans.y2012.coreservlet.CatalogItem;
import com.mec.beans.y2012.coreservlet.ItemOrder;

@WebServlet("/OrderPageServlet")
public class OrderPageServlet extends HttpServlet {

	private static final long serialVersionUID = -5081144988985546378L;

	public static final String SHOPPING_CART = "shoppingCart";
	public static final String NUM_ITEMS = "numItems";
	public static final String ITEM_ID = "itemId";
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		HttpSession session = req.getSession();
		//
		ShoppingCart cart;
		synchronized(session){
			cart = (ShoppingCart) session.getAttribute(SHOPPING_CART);
			if(null == cart){
				cart = new ShoppingCart();
				session.setAttribute(SHOPPING_CART, cart);
			}
			//
			String itemId = req.getParameter(ITEM_ID);
			if(null != itemId){
				String numItemsString = req.getParameter(NUM_ITEMS);
				
				if(null == numItemsString){
					cart.addItem(itemId);
				}else{
					int numItems;
					try{
						numItems = Integer.parseInt(numItemsString);
					}catch(NumberFormatException nfe){
						numItems = 1;
					}
					//
					cart.setNumOrdered(itemId, numItems);
				}
			}
		}
		
		//
		resp.setContentType("text/html");
		PrintWriter out = resp.getWriter();
		//
		String title = "Status of Your Order";
		out.println(Constants.docType + ServletUtilities.headWithTitle(title) +
				"<body><h1>" + title + "</h1>"
				);
		
		synchronized(session){
			List<ItemOrder> itemsOrdered = cart.getItemsOrdered();
			if(itemsOrdered.isEmpty()){
				out.println("<h2><em>No Items in your cart..</em></h2>");
			}else{
				out.println("<table border=\"1\"><tr><th>Item Id</th><th>Unit Cost</th><th>Number</th><th>Total Cost</th></tr>");
				//
				NumberFormat formatter = NumberFormat.getCurrencyInstance();
				for(ItemOrder itemOrder : itemsOrdered){
					CatalogItem item = itemOrder.getItem();
					//
					out.println("<tr><td>" + item.getItemId() + "</td><td>" + item.getShortDescription() + "</td><td>" + 
					formatter.format(item.getCost()) + "</td><td><form><input type = \"hidden\" name=\"" + ITEM_ID + "\" value =\"" + 
					item.getItemId() + "\"/><input type=\"text\" name=\"" + NUM_ITEMS + "\" size=3 value=\"" + 
					itemOrder.getNumItems() + "\"/><input type = \"submit\" value=\"Update Order\"/></form></td>" +
					"<td>" + formatter.format(itemOrder.getTotalCost()) + "</td>"
					);
				}
				//
				String checkoutURL = resp.encodeURL("Checkout.html");
				out.println("</table><form action=\"" + checkoutURL + "\"><input type = \"submit\" value=\"Proceed to Checkout\"/></form>");
			}
		}
		//
		out.println("</body></html");
	}


	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doGet(req, resp);
	}


	private static final Logger logger = 
			Logger.getLogger(OrderPageServlet.class);
}
